﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mogre;

namespace ProjetSMA.View.Mogre
{
    public class LoadingBar
    {
        protected RenderWindow window;
        protected Overlay loadOverlay;
        protected float initProportion;
        protected ushort numGroupsInit;
        protected ushort numGroupsLoad;
        protected float progressBarMaxSize;
        protected float progressBarScriptSize;
        protected float progressBarInc;
        protected OverlayElement loadingBarElement;
        protected OverlayElement loadingDescriptionElement;
        protected OverlayElement loadingCommentElement;

        public void Start(RenderWindow window, ushort numGroupsInit, ushort numGroupsLoad, float initProportion)
        {
            this.window = window;
            this.numGroupsInit = numGroupsInit;
            this.numGroupsLoad = numGroupsLoad;
            this.initProportion = initProportion;

            ResourceGroupManager.Singleton.InitialiseResourceGroup("Bootstrap");

            OverlayManager omgr = OverlayManager.Singleton;
            loadOverlay = omgr.GetByName("Core/LoadOverlay");
            loadOverlay.Show();

            loadingBarElement = omgr.GetOverlayElement("Core/LoadPanel/Bar/Progress");
            loadingCommentElement = omgr.GetOverlayElement("Core/LoadPanel/Comment");
            loadingDescriptionElement = omgr.GetOverlayElement("Core/LoadPanel/Description");

            OverlayElement barContainer = omgr.GetOverlayElement("Core/LoadPanel/Bar");
            progressBarMaxSize = barContainer.Width;
            loadingBarElement.Width = 0F;

            ResourceGroupManager.Singleton.ResourceGroupScriptingStarted += ResourceGroupScriptingStarted;
            ResourceGroupManager.Singleton.ScriptParseStarted += ScriptParseStarted;
            ResourceGroupManager.Singleton.ScriptParseEnded += ScriptParseEnded;
            ResourceGroupManager.Singleton.ResourceGroupLoadStarted += ResourceGroupLoadStarted;
            ResourceGroupManager.Singleton.ResourceLoadStarted += ResourceLoadStarted;
            ResourceGroupManager.Singleton.WorldGeometryStageStarted += WorldGeometryStageStarted;
            ResourceGroupManager.Singleton.WorldGeometryStageEnded += WorldGeometryStageEnded;
        }

        public void Finish()
        {
            // hide loading screen
            loadOverlay.Hide();

            ResourceGroupManager.Singleton.WorldGeometryStageEnded -= WorldGeometryStageEnded;
            ResourceGroupManager.Singleton.WorldGeometryStageStarted -= WorldGeometryStageStarted;
            ResourceGroupManager.Singleton.ResourceLoadStarted -= ResourceLoadStarted;
            ResourceGroupManager.Singleton.ResourceGroupLoadStarted -= ResourceGroupLoadStarted;
            ResourceGroupManager.Singleton.ScriptParseEnded -= ScriptParseEnded;
            ResourceGroupManager.Singleton.ScriptParseStarted -= ScriptParseStarted;
            ResourceGroupManager.Singleton.ResourceGroupScriptingStarted -= ResourceGroupScriptingStarted;
        }

        void ResourceGroupScriptingStarted(string groupName, uint scriptCount)
        {
            progressBarInc = progressBarMaxSize * initProportion / (float)scriptCount;
            progressBarInc /= numGroupsInit;
            loadingDescriptionElement.Caption = "Interpretation des scripts...";
            window.Update();
        }

        void ScriptParseStarted(string scriptName, out bool skipThisScript)
        {
            loadingCommentElement.Caption = scriptName;
            window.Update();

            skipThisScript = false;
        }

        void ScriptParseEnded(string scriptName, bool skipped)
        {
            loadingBarElement.Width += progressBarInc;
            window.Update();
        }

        void ResourceGroupLoadStarted(string groupName, uint resourceCount)
        {
            progressBarInc = progressBarMaxSize * (1 - initProportion) / (float)resourceCount;
            progressBarInc /= numGroupsLoad;
            loadingDescriptionElement.Caption = "Chargement des ressources...";
            window.Update();
        }

        void ResourceLoadStarted(ResourcePtr resource)
        {
            loadingCommentElement.Caption = resource.Name;
            window.Update();
        }

        void WorldGeometryStageStarted(string description)
        {
            loadingCommentElement.Caption = description;
            window.Update();
        }

        void WorldGeometryStageEnded()
        {
            loadingBarElement.Width += progressBarInc;
            window.Update();
        }
    }
}
